package com.bootdo.system.task;

import com.bootdo.system.domain.BizhongDO;
import com.bootdo.system.domain.FrontuserDO;
import com.bootdo.system.domain.JiaoyicelueDO;
import com.bootdo.system.service.BizhongService;
import com.bootdo.system.service.FrontlogService;
import com.bootdo.system.service.FrontuserService;
import com.bootdo.system.service.JiaoyicelueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
public class ScheduleTask {

    @Autowired
    private FrontuserService frontuserService;
    @Autowired
    private JiaoyicelueService jiaoyicelueService;
    @Autowired
    private BizhongService bizhongService;
    @Autowired
    private FrontlogService frontlogService;

    //3.添加定时任务 每小时   //5秒 */5 * * * * ?   小时0 0 * * * ?
    @Scheduled(cron = "0 0 * * * ?")
    public void configureTasks() {

        log.info("减少每个用户有效时间1小时定时任务：状态：开始");
        List<FrontuserDO> frontuserDOS = frontuserService.list(new HashMap<>());

        //如果用户金额少于10或者有效时间小于0，停止所有交易

        log.info("判断用户是否有效定时任务：状态 开始");
        for (FrontuserDO u:frontuserDOS) {
            //有效时间
            if(u.getYouxiaotime()==null||u.getYouxiaotime()<=0){
                jiaoyicelueService.stopAll(u.getUserid());
                frontlogService.savelog(u.getUserid(),"用户 "+u.getUsername()+" 所有自动化交易停止。原因：有效时间小于等于0");
                log.info("用户 "+u.getUsername()+" 所有自动化交易停止。原因：有效时间小于等于0");
                continue;
            }
            if(u.getZichan()==null||u.getZichan().compareTo(BigDecimal.TEN)<0){
                jiaoyicelueService.stopAll(u.getUserid());
                log.info("用户 "+u.getUsername()+" 所有自动化交易停止。原因：账户资金小于10USDT");
                frontlogService.savelog(u.getUserid(),"用户 "+u.getUsername()+" 所有自动化交易停止。原因：账户资金小于10USDT");
                continue;
            }
            if(u.getYouxiaotime()>0){
                u.setYouxiaotime(u.getYouxiaotime()-1);
                u.setLoginpass(null);
                u.setPaypass(null);
                frontuserService.update(u);
            }


        }

        log.info("判断用户是否有效定时任务：状态 完成");

    }


}
